package com.vn.service;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


import com.vn.entity.Building;
import com.vn.entity.BuildingType;
import com.vn.entity.ChargeRegister;
import com.vn.entity.ChargeRegisterPayment;
import com.vn.entity.Compartment;
import com.vn.entity.CompartmentType;
import com.vn.entity.Customer;
import com.vn.entity.Interest;
import com.vn.entity.Land;
import com.vn.entity.Location;
import com.vn.entity.PaymentInstallment;
import com.vn.entity.PaymentMode;
import com.vn.entity.PurchaseContract;
import com.vn.entity.Storage;
import com.vn.entity.StoreDetail;


@Service
@Transactional
public class MyService {
	@Autowired
	private SessionFactory sessionFactory;
	
	public void addStorage(Storage storage) {
		sessionFactory.getCurrentSession().save(storage);
	}
	
	public Storage getStorageByName(String name)
	{
		return (Storage) sessionFactory.getCurrentSession().createCriteria(Storage.class)
		.add(Restrictions.eq("folderName", name))
		.uniqueResult();
	}
	
	public void addStorageDetail(StoreDetail storeDetail)
	{
		sessionFactory.getCurrentSession().save(storeDetail);
		
	}
	
	public void addLocation(Location loc)
	{
		sessionFactory.getCurrentSession().save(loc);
		
	}
	
	public Location getLocationById(int id)
	{
		return (Location)sessionFactory.getCurrentSession().createCriteria(Location.class)
		.add(Restrictions.eq("id", id))
		.uniqueResult();
	}
	
	public void addLand(Land land)
	{
		sessionFactory.getCurrentSession().save(land);
	}
	
	public void addBuildingType(BuildingType type)
	{
		sessionFactory.getCurrentSession().save(type);
	}
	
	public BuildingType getBuildingTypeById(short id)
	{
		return (BuildingType)sessionFactory.getCurrentSession().createCriteria(BuildingType.class)
				.add(Restrictions.eq("id", id))
				.uniqueResult();
		
	}
	
	public Land getLandById(int id)
	{
		return (Land)sessionFactory.getCurrentSession().createCriteria(Land.class)
				.add(Restrictions.eq("id", id))
				.uniqueResult();
	}

	public void addBuilding(Building building)
	{
		sessionFactory.getCurrentSession().save(building);
	}
	
	public Building getBuildingById(String id)
	{
		return (Building)sessionFactory.getCurrentSession().createCriteria(Building.class)
				.add(Restrictions.eq("id", id))
				.uniqueResult();
	}
	
	public void addCompartmentType(CompartmentType compartmentType)
	{
		sessionFactory.getCurrentSession().save(compartmentType);
	}
	
	public CompartmentType getCompartmentTypeById(int id)
	{
		return (CompartmentType)sessionFactory.getCurrentSession().createCriteria(CompartmentType.class)
				.add(Restrictions.eq("id", id))
				.uniqueResult();
		
	}
	
	public void addCompartment(Compartment compartment)
	{
		sessionFactory.getCurrentSession().save(compartment);
	}
	
	public Compartment getCompartmentById(String id)
	{
		return (Compartment) sessionFactory.getCurrentSession().createCriteria(Compartment.class)
		.add(Restrictions.eq("id", id)).uniqueResult();
	}
	
	public void addPaymentMode(PaymentMode paymentMode)
	{
		sessionFactory.getCurrentSession().save(paymentMode);
	}
	
	public PaymentMode getPaymentModeById(int id){
		return (PaymentMode) sessionFactory.getCurrentSession().createCriteria(PaymentMode.class)
		.add(Restrictions.eq("id", id)).uniqueResult();
	}
	
	public void addInterest(Interest interest)
	{
		sessionFactory.getCurrentSession().save(interest);
	}
	
	public void addCustomer(Customer customer)
	{
		sessionFactory.getCurrentSession().save(customer);
	}
	
	public Customer getCustomerById(String id)
	{
		return (Customer) sessionFactory.getCurrentSession().createCriteria(Customer.class)
		.add(Restrictions.eq("id", id)).uniqueResult();
	}
	
	public void addChargeRegister(ChargeRegister chargeRegister)
	{
		sessionFactory.getCurrentSession().save(chargeRegister);
	}
	
	public ChargeRegister getChargeRegisterById(int id)
	{
		return (ChargeRegister)sessionFactory.getCurrentSession().createCriteria(ChargeRegister.class)
		.add(Restrictions.eq("id", id)).uniqueResult();
	}
	
	public void addPurchaseContract(PurchaseContract purchaseContract)
	{
		sessionFactory.getCurrentSession().save(purchaseContract);
	}
	
	public PurchaseContract getPurchaseContractById(int id)
	{
		return (PurchaseContract)sessionFactory.getCurrentSession().createCriteria(PurchaseContract.class)
		.add(Restrictions.eq("id", id)).uniqueResult();
	}
	
	public void addPaymentInstallment(PaymentInstallment paymentInstallment)
	{
		sessionFactory.getCurrentSession().save(paymentInstallment);
	}
	
	public void addChargeRegisterPayment(ChargeRegisterPayment chargeRegisterPayment)
	{
		sessionFactory.getCurrentSession().save(chargeRegisterPayment);
	}
}
